package com.kinon.tyjaccount.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.kinon.tyjaccount.data.UPacketFactory;
import com.kinon.tyjaccount.tools.HttpTools;
import com.kinon.tyjaccount.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import sunmi.ds.DSKernel;
import sunmi.ds.callback.IConnectionCallback;
import sunmi.ds.callback.IReceiveCallback;
import sunmi.ds.data.DSData;
import sunmi.ds.data.DSFile;
import sunmi.ds.data.DSFiles;
import sunmi.ds.data.DataPacket;

/**早晚餐支付成功*/

public class PayresultActivity extends Activity {
	
	private TextView tv_jine;
	private TextView tv_fanhui;
	private EditText et_pay;
	String id;
	String token;
	String balance;
	
	HttpTools httptools=null;

	private static final String SHOW_IMG_LIST_ID = "SHOW_IMG_LIST_ID";
	private static final String WELCOME_IMG_ID2 = "WELCOME_IMG_ID2";

	private final static String SHOW_VIDEO_LIST_DATA_ID = "SHOW_VIDEO_LIST_DATA_ID";
	private final static String SHOW_IMGS_LIST_DATA_ID = "SHOW_IMGS_LIST_DATA_ID";
	long taskId;
	String filePath = Environment.getExternalStorageDirectory().getPath() + "/picture/dcim/chenggong.png";
	String pathBig = Environment.getExternalStorageDirectory().getPath() + "/picture/dcim/chenggong.png";

	DSKernel mDSKernel;// SDK核心类
	DataPacket dsPacket;
	long fileId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payresult);
		initRes();
		Show();
//		initSDK();
	}

//	@Override
//	protected void onStart() {
//		super.onStart();
//		showWelcomeImage();
////		Show();
//	}


	private void Show(){
		DSKernel mDSKernel = DSKernel.newInstance();
		mDSKernel.init(getApplicationContext(), new IConnectionCallback() {
			@Override
			public void onDisConnect() {

			}

			@Override
			public void onConnected(ConnState state) {

			}
		});
		mDSKernel.addReceiveCallback(new IReceiveCallback() {
			@Override
			public void onReceiveData(DSData data) {

			}

			@Override
			public void onReceiveFile(DSFile file) {

			}

			@Override
			public void onReceiveFiles(DSFiles files) {

			}

			@Override
			public void onReceiveCMD(DSData cmd) {

			}
		});
		JSONObject json = new JSONObject();
		try {
			json.put("title", "支付成功");
			json.put("content","余额：￥ "+balance);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		DataPacket packet = UPacketFactory.buildShowText(DSKernel.getDSDPackageName(), json.toString(),null);
		mDSKernel.sendData(packet);
	}


	/**以下为副屏显示*/
//	private void initSDK() {
//		mDSKernel = DSKernel.newInstance();
//		mDSKernel.init(this, mConnCallback);
////		showWelcomeImage();
//	}
//
//	IConnectionCallback mConnCallback = new IConnectionCallback() {// SDK链接状态回调
//		@Override
//		public void onDisConnect() {
//		}
//
//		@Override
//		public void onConnected(final ConnState state) {
//			runOnUiThread(new Runnable() {
//				@Override
//				public void run() {
////					Toast.makeText(MainActivity.this, "state:"+state, Toast.LENGTH_LONG).show();
//					switch (state) {
//						case AIDL_CONN:
//							//与本地service的连接畅通
//							break;
//						case VICE_SERVICE_CONN:
//							//与副屏service连接畅通
//							long fileId = SharedPreferencesUtil.getLong(PayresultActivity.this, SHOW_IMG_LIST_ID);
//							long fileId1 = SharedPreferencesUtil.getLong(PayresultActivity.this, WELCOME_IMG_ID2);
//							long fileId2 = SharedPreferencesUtil.getLong(PayresultActivity.this,SHOW_VIDEO_LIST_DATA_ID);
//							long fileId3 = SharedPreferencesUtil.getLong(PayresultActivity.this,SHOW_IMGS_LIST_DATA_ID);
//							if(fileId2!=-1L)checkImg(fileId2,SHOW_VIDEO_LIST_DATA_ID);
//							if(fileId3!=-1L)checkImg(fileId3,SHOW_IMGS_LIST_DATA_ID);
//							if(fileId!=-1L)checkImg(fileId, SHOW_IMG_LIST_ID);
//							if(fileId1!=-1L) checkImg(fileId1, WELCOME_IMG_ID2);
//							break;
//						case VICE_APP_CONN:
//							//与副屏app连接畅通
//							break;
//
//						default:
//							break;
//					}
//
//				}
//			});
//		}
//	};
//	private void checkImg(long fileId, final String key){
//
//		mDSKernel.checkFileExist(fileId, new ICheckFileCallback() {
//			@Override
//			public void onCheckFail() {
////				Toast.makeText(MainActivity.this, key + " 文件不存在", Toast.LENGTH_LONG).show();
//			}
//
//			@Override
//			public void onResult(boolean b) {
//				if(!b){
//					SharedPreferencesUtil.put(PayresultActivity.this, key, -1L);
//				}else{
////					Toast.makeText(MainActivity.this, key + " 文件存在", Toast.LENGTH_LONG).show();
//				}
//			}
//		});
//	}
//	private void showWelcomeImage() {
//		taskId = SharedPreferencesUtil.getLong(this, WELCOME_IMG_ID2);
//		if (taskId != -1L) {
//			showImgWelcome(taskId);
//			return;
//		}
//
//		File file = new File(pathBig);
//		if (!file.exists()) {
//			Toast.makeText(getApplicationContext(), "No file!", Toast.LENGTH_SHORT).show();
//			return;
//		}
//		taskId = mDSKernel.sendFile(DSKernel.getDSDPackageName(), pathBig, new ISendCallback() {
//			@Override
//			public void onSendSuccess(long l) {
//				showImgWelcome(taskId);
//				SharedPreferencesUtil.put(getApplicationContext(), WELCOME_IMG_ID2, taskId);
//			}
//
//			@Override
//			public void onSendFail(int i, String s) {
//			}
//
//			@Override
//			public void onSendProcess(long l, long l1) {
//			}
//		});
//	}

//	private void showImgWelcome(long taskId) {
//		String json1 = UPacketFactory.createJson(DataModel.SHOW_IMG_WELCOME,"");
//		mDSKernel.sendCMD(DSKernel.getDSDPackageName(),json1,taskId,null);
//	}

	private void initRes() {
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("data", "");
		tv_jine=(TextView)findViewById(R.id.tv_jine);
		balance = getIntent().getStringExtra("balance");
//		Log.i("zq", "金额----"+jine);
		tv_fanhui=(TextView)findViewById(R.id.tv_fanhui);
		tv_fanhui.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
//		Show();
		handler.postDelayed(runnable, 3000);
		Log.i("zq","start");
	}

//	private void Show(){
//		final DSKernel mDSKernel = DSKernel.newInstance();
//		mDSKernel.init(getApplicationContext(), new IConnectionCallback() {
//			@Override
//			public void onDisConnect() {
//
//			}
//
//			@Override
//			public void onConnected(ConnState state) {
//
//			}
//		});
//		mDSKernel.addReceiveCallback(new IReceiveCallback() {
//			@Override
//			public void onReceiveData(DSData data) {
//
//			}
//
//			@Override
//			public void onReceiveFile(DSFile file) {
//
//			}
//
//			@Override
//			public void onReceiveFiles(DSFiles files) {
//
//			}
//
//			@Override
//			public void onReceiveCMD(DSData cmd) {
//
//			}
//		});
//		mDSKernel.sendFile(DSKernel.getDSDPackageName(), filePath, new ISendCallback() {
//			@Override
//			public void onSendSuccess(long taskId) {
//				showImg(taskId);//先发送图片
//			}
//
//			@Override
//			public void onSendFail(int errorId, String errorInfo) {
//
//			}
//
//			@Override
//			public void onSendProcess(long totle, long sended) {
//
//			}
//		});
//
//	}
//	void showImg(long fileId){
//		String json = UPacketFactory.createJson(DataModel.SHOW_IMG_WELCOME, "def");
//		mDSKernel.sendCMD(DSKernel.getDSDPackageName(), json, fileId, null);
//	}

	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
	       super.onWindowFocusChanged(hasFocus);
	       if (hasFocus) {
	            View decorView = getWindow().getDecorView();
	                 decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
	                  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
	                  | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
	                  |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
	                  |View.SYSTEM_UI_FLAG_FULLSCREEN);
	               }
	}

	private Handler handler = new Handler();
	private Runnable runnable = new Runnable() {
		public void run() {
			try {
				this.update();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			handler.postDelayed(this, 3000);// 3妙关闭
		}
		void update() throws IOException {
//			onrefresh.onPullDownToRefresh(list_order);
			finish();
//			handler.removeCallbacks(runnable);
			Log.i("zq","finsh");
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler.removeCallbacks(runnable);
	}
}
